package edu.xust.dao.impl;


import edu.xust.config.Repository;
import edu.xust.dao.TStudentDao;
import edu.xust.entity.TStudent;
import edu.xust.entity.TStudentExample;
import edu.xust.mapper.TStudentMapper;
import edu.xust.page.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("tStudentDao")
public class TStudentDaoImpl implements TStudentDao {
    static TStudentMapper tStudentMapper;
    static {
        Reader reader = null;//读取根目录下的confi.xml
        try {
            reader = Resources.getResourceAsReader("config.xml");

            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            SqlSession sqlSession = sqlSessionFactory.openSession();//通过连接会话工厂得到一个连接
            tStudentMapper = sqlSession.getMapper(TStudentMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<TStudent> getTStudentByPage(PageInfo pageInfo) throws Exception {
        TStudentExample tStudentExample = new TStudentExample();
        long l = tStudentMapper.countByExample(null);
        pageInfo.setItemCount((int)l);
        tStudentExample.setOffset(pageInfo.getOffset());
        tStudentExample.setLimit(pageInfo.getPageSize());
        return tStudentMapper.selectByExample(tStudentExample);
    }

    @Override
    public List<TStudent> getTStudentByPageOfWaystwo(PageInfo pageInfo) throws Exception {

       /* pageInfo.setCurrPage(3);
        pageInfo.setPageSize(3);*/
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        List<TStudent> tStudentByPage = tStudentMapper.getTStudentByPage(map);
        return tStudentByPage;
    }
}
